<?php
/**
 * Created by PhpStorm.
 * User: Jiarui
 * Date: 2015/11/18
 * Time: 11:51
 */

namespace Admin\Model;
use Org\Util\ArrayList;
use Think\Model;
use Think\Log;

class SettingModel extends Model
{
    public function getHomePage(){

        $homePage =  new \stdClass();

        $homePage1 = $this->where(array('key_word'=>'homePage1'))->find();

        $homePage->data['homePage1'] = $homePage1['value'];

        $homePage2 = $this->where(array('key_word'=>'homePage2'))->find();

        $homePage->data['homePage2'] = $homePage2['value'];

        $homePage3 = $this->where(array('key_word'=>'homePage3'))->find();

        $homePage->data['homePage3'] = $homePage3['value'];

        $homePage4 = $this->where(array('key_word'=>'homePage4'))->find();

        $homePage->data['homePage4'] = $homePage4['value'];

        return $homePage->data;

    }

    public function saveHomePage($type,$img_rul,$route_id){

        if($type == 1){
            $condition = array('key_word'=>'homePage1');
            $add = array('key_word'=>'homePage1','value'=>$img_rul,'route_id'=>$route_id);
        }elseif($type == 2){
            $condition = array('key_word'=>'homePage2');
            $add = array('key_word'=>'homePage2','value'=>$img_rul,'route_id'=>$route_id);
        }elseif($type == 3){
            $condition = array('key_word'=>'homePage3');
            $add = array('key_word'=>'homePage3','value'=>$img_rul,'route_id'=>$route_id);
        }elseif($type == 4){
            $condition = array('key_word'=>'homePage4');
            $add = array('key_word'=>'homePage4','value'=>$img_rul,'route_id'=>$route_id);
        }
        $data = array('value'=>$img_rul,'route_id'=>$route_id);

        $isExit = $this->where($condition)->find();

        if($isExit) {
            $result = $this->where($condition)->data($data)->save();
        }else{
            $result = $this->data($add)->add();
        }

        if($result){
            if($type == 1){
                $where = array('key_word'=>'homePage1');
            }elseif($type == 2){
                $where = array('key_word'=>'homePage2');
            }elseif($type == 3){
                $where = array('key_word'=>'homePage3');
            }elseif($type == 4){
                $where = array('key_word'=>'homePage4');
            }
            $ret = $this->where($where)->find();

            return $ret;
        }else{
            return 'fail';
        }

    }

    public function deletePage($type){

        if($type == 1){
            $isExist = $this->where(array('key_word'=>'homePage1'))->find();

            if($isExist){

                $this->where(array('key_word'=>'homePage1'))->delete();

                return 'ok';

            }else{

                return 'noExist';
            }

            return 'fail';
        }elseif($type == 2){

            $isExist = $this->where(array('key_word'=>'homePage2'))->find();

            if($isExist){

                $this->where(array('key_word'=>'homePage2'))->delete();

                return 'ok';

            }else{

                return 'noExist';
            }

            return 'fail';

        }elseif($type == 3){

            $isExist = $this->where(array('key_word'=>'homePage3'))->find();

            if($isExist){

                $this->where(array('key_word'=>'homePage3'))->delete();

                return 'ok';

            }else{

                return 'noExist';
            }

            return 'fail';

        }elseif($type == 4){

            $isExist = $this->where(array('key_word'=>'homePage4'))->find();

            if($isExist){

                $this->where(array('key_word'=>'homePage4'))->delete();

                return 'ok';

            }else{

                return 'noExist';
            }

            return 'fail';

        }

    }

    public function updateCode($tmp_name){

        $setting = M('setting');

        if(intval($tmp_name) == -1){

            return false;

        }else{

            if($tmp_name != '') {

                $tmp_path = __PUBLIC__ . DIRECTORY_SEPARATOR . 'uploads_tmp' . DIRECTORY_SEPARATOR . 'system' . DIRECTORY_SEPARATOR . $tmp_name;

                $tar_path = __PUBLIC__ . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'systemImage' . DIRECTORY_SEPARATOR . $tmp_name;

                @rename($tmp_path, $tar_path);

                $data['value'] = $tmp_name;

            }
            Log::write(json_encode($data),'WARN');
        }

        $tempSetting = $setting->where(array("key_word"=>'codeGZ'))->find();

        if($tempSetting){
            @unlink(__PUBLIC__ . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'systemImage' . DIRECTORY_SEPARATOR . $tempSetting['value']);
            $result = $setting->where(array("key_word"=>'codeGZ'))->save($data);
        }else{
            $data['route_id']=0;
            $data['key_word']='codeGZ';
            $result = $setting->add($data);
        }

        if($result)
            return true;
        else
            return false;

    }

    public function getCodeGZ(){
        $setting = M('setting');

        $data = $setting->where(array('key_word'=>'codeGZ'))->find();

        return $data['value'];
    }
}